{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class AppendableList(list):\n",
    "    \"\"\"\n",
    "    Python list with additional property '.new_item', which supports only setting, not getting.\n",
    "    \n",
    "    Example: \n",
    "    >>> x = AppendableList()\n",
    "    >>> x.new_item = 4\n",
    "    >>> x.new_item = 2\n",
    "    >>> x\n",
    "    Out: [4, 2]\n",
    "    \n",
    "    Intended to be used during unpacking tuples.\n",
    "    >>> weights, heights = AppendableList(), AppendableList()\n",
    "    >>> for person in some_people_base():\n",
    "    >>>     weights.new_item, heights.new_item = person.get_weight_and_height()    \n",
    "    \"\"\"\n",
    "    \n",
    "    def __setattr__(self, name, value):\n",
    "        \"\"\"Handling setting new_item. Pay attention, that this is only a readable property \"\"\"\n",
    "        if name == 'new_item':\n",
    "            # appending new item to the end of list\n",
    "            self.append(value)\n",
    "        super(AppendableList, self).__setattr__(name, value)\n",
    "        \n",
    "    def __dir__(self):\n",
    "        \"\"\"Method to list available fields. Adding new_item\"\"\"\n",
    "        return dir(list) + ['new_item']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mocks for functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy\n",
    "def iterate_minibatches(X_val, y_val, batch_size, shuffle):\n",
    "    for _ in range(10000):\n",
    "        yield 1, 2\n",
    "\n",
    "def val_fn(inputs, targets):\n",
    "    return numpy.random.normal() * 0.1 + 0.1, numpy.random.normal() * 0.1 + 0.5\n",
    "\n",
    "X_val = None \n",
    "y_val = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  validation loss:\t\t0.099111\n",
      "  validation accuracy:\t\t50.23 %\n"
     ]
    }
   ],
   "source": [
    "# And a full pass over the validation data:\n",
    "val_err = 0\n",
    "val_acc = 0\n",
    "val_batches = 0\n",
    "for batch in iterate_minibatches(X_val, y_val, 500, shuffle=False):\n",
    "    inputs, targets = batch\n",
    "    err, acc = val_fn(inputs, targets)\n",
    "    val_err += err\n",
    "    val_acc += acc\n",
    "    val_batches += 1\n",
    "\n",
    "# Then we print the results for this epoch:\n",
    "\n",
    "# < I've deleted some lines not important here >\n",
    "\n",
    "print(\"  validation loss:\\t\\t{:.6f}\".format(val_err / val_batches))\n",
    "print(\"  validation accuracy:\\t\\t{:.2f} %\".format(val_acc / val_batches * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  validation accuracy:\t\t30.01 %\n"
     ]
    }
   ],
   "source": [
    "validation_accuracies = [val_fn(inputs, targets) for inputs, targets\n",
    "                         in iterate_minibatches(X_val, y_val, 500, shuffle=False)]\n",
    "print(\"  validation accuracy:\\t\\t{:.2f} %\".format(numpy.mean(validation_accuracies) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  validation loss:\t\t0.100079\n",
      "  validation accuracy:\t\t50.03 %\n"
     ]
    }
   ],
   "source": [
    "validation_losses_and_accuracies = [val_fn(inputs, targets) for inputs, targets\n",
    "                                    in iterate_minibatches(X_val, y_val, 500, shuffle=False)]\n",
    "validation_losses, validation_accuracies = zip(*validation_losses_and_accuracies)\n",
    "\n",
    "print(\"  validation loss:\\t\\t{:.6f}\".format(numpy.mean(validation_losses)))\n",
    "print(\"  validation accuracy:\\t\\t{:.2f} %\".format(numpy.mean(validation_accuracies) * 100.))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  validation loss:\t\t0.098584\n",
      "  validation accuracy:\t\t50.00 %\n"
     ]
    }
   ],
   "source": [
    "validation_losses = AppendableList()\n",
    "validation_accuracies = AppendableList()\n",
    "\n",
    "for inputs, targets in iterate_minibatches(X_val, y_val, 500, shuffle=False):\n",
    "    validation_losses.new_item, validation_accuracies.new_item = val_fn(inputs, targets)\n",
    "    \n",
    "print(\"  validation loss:\\t\\t{:.6f}\".format(numpy.mean(validation_losses)))\n",
    "print(\"  validation accuracy:\\t\\t{:.2f} %\".format(numpy.mean(validation_accuracies) * 100.))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.49998718083393917"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numpy.mean(validation_accuracies)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = AppendableList()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
